package cn.itcast.controller.company;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.company.Company;
import cn.itcast.domain.system.Module;
import cn.itcast.service.company.CompanyService;
import cn.itcast.service.system.ModuleService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Reference
    private CompanyService companyService;

    @RequestMapping(value = "/list",name ="查询所有企业数据")
    @RequiresPermissions("企业管理") // 如果当前登录人有“企业管理”的权限才能进入这个方法
    public String findAll(@RequestParam(defaultValue = "1",name = "page") int pageNum,@RequestParam(defaultValue = "2") int pageSize){
//        List<Company> companyList = companyService.findAll();
//        PageBean pageBean = companyService.findPage(pageNum,pageSize);
        PageInfo<Company> pageBean = companyService.findPage(pageNum,pageSize);

        request.setAttribute("page",pageBean);
        return "company/company-list";
    }
    @RequestMapping(value = "/toAdd",name ="进入到添加企业的页面")
    public String toAdd(){
        return "company/company-add";
    }

    @RequestMapping(value = "/edit",name ="保存企业数据") //新增或修改时都会进入这个方法
    public String edit(Company company){

        if(StringUtils.isEmpty(company.getId())){
            //id为空新增 save
            company.setId(UUID.randomUUID().toString());
            companyService.save(company);
        }else{
            companyService.update(company); //id如果有值 update
        }
//        不管新增还是修改都是要重定向到列表页面
        return "redirect:/company/list.do";  //redirect 重定向  在浏览器发起的请求
    }

    @RequestMapping(value = "/toUpdate",name ="进入到编辑页面")
    public String toUpdate(String id){
        Company company = companyService.findById(id);
        request.setAttribute("company",company);
        return "company/company-add";
    }
    @RequestMapping(value = "/delete",name ="根据id删除企业数据")
    public String delete(String id){
        companyService.delete(id);

        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/companyModule",name ="进入到给企业分配模块的页面")
    public String companyModule(String companyId){
        Company company = companyService.findById(companyId);
        request.setAttribute("company",company);
        return "company/company-module";
    }

    @Autowired
    private ModuleService moduleService;


    @RequestMapping(value = "/getZtreeNodes",name ="获取树形节点数据")
    @ResponseBody  // 转json+把数据返回到浏览器
    public List<Map> getZtreeNodes(String companyId){
//        查询当前企业下的菜单ids  select  module_id  from  ss_company_module where conpany_id=?
        List<String> roleModuleIdList = companyService.findModuleLisByCompanyId(companyId);  // 菜单id的集合

//        树形节点的数据是从哪里来的？ 从菜单表中获取的
        List<Module> moduleList = moduleService.findAll();
//        重新构建一个集合
        List<Map> ztreeNodes = new ArrayList<Map>();
        Map map = null;
        for (Module module : moduleList) {
            map = new HashMap<>();
            map.put("id",module.getId());
            map.put("name",module.getName());
            map.put("pId",module.getParentId());
//            判断roleModuleIdList是否包含 module.getId()
            if(roleModuleIdList.contains(module.getId())){
                map.put("checked",true);    //判断当前角色下的菜单权限是否包含当前的菜单权限
            }
            ztreeNodes.add(map);
        }
        return ztreeNodes;
    }


    @RequestMapping(value = "/updateCompanyModule",name ="给企业分配菜单的方法")
    public String updateCompanyModule(String companyId,String moduleIds){  //moduleIds="2,201"
        companyService.updateCompanyModule(companyId,moduleIds);  // 从数据的本质上 就是向中间表插入数据
        return "redirect:/company/list.do";  //完成菜单权限分配后 重定向到企业的列表页面
    }
}
